<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>部门名</label>
                    <div class="layui-input-block">
                        <input type="text" name="deptName" lay-verify="deptName" autocomplete="off" placeholder="请输入部门名"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>排序</label>
                    <div class="layui-input-block">
                        <input type="text" name="sort" lay-verify="sort" autocomplete="off" placeholder="请输入排序"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="正常" checked>
                        <input type="radio" name="status" value="0" title="停用">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        上级部门
                    </label>
                    <div class="layui-input-block">
                        <input type="number" id="parentId" name="parentId" lay-verify="parentId"
                               style="display:none;width: 0px" autocomplete="off" class="layui-input">
                        <ul id="dataTree" class="dtree" data-id="0"></ul>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="dept-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['dtree', 'form', 'jquery'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        // 初始化树
        dtree.render({
            elem: "#dataTree",
            initLevel: "1",//默认展开层级为1
            width: "100%",
            method: 'get',
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            // response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
            url: "/dept/buildDeptAll",
            dataFormat: "list",  //配置data的风格为list
            select: true, //指定下拉树模式
            selectTips: "不选默认是顶级目录",
            selectCardHeight: "150"
        });
        dtree.on("node('dataTree')", function (obj) {
            var param = dtree.getNowParam("dataTree");
            $("#parentId").val(param.nodeId);
        });
        form.verify({
            deptName: function (value) {
                if (value.length < 2) {
                    return '部门名至少2个字符';
                }
            },
            sort: [
                /^[1-9]\d*$/
                , '只能是整数哦'
            ],
        })
        form.on('submit(dept-save)', function (data) {
            let loading = layer.load();
            $.ajax({
                url: '/dept/add',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    layer.close(loading);
                    if(result.code == 200){
                        layer.msg(result.message, {icon: 1, time: 3000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.location.reload();
                        });
                    } else {
                        layer.msg(result.message, {icon: 2, time: 3000});
                    }
                }
            })
            return false;
        });
    })
</script>
</body>
</html>